package com.liangzhong.proxy.trainingcamp
{
	import com.common.utils.Debug;
	import com.liangzhong.commandconst.commandType.MainCommandType;
	import com.liangzhong.commandconst.logic.CMD_Logic;
	import com.liangzhong.commandconst.training.SMDTraining;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.playerCenter.vo.PlayerVo;
	import com.liangzhong.model.trainingcamp.TrainingcampDataManager;
	import com.liangzhong.model.type.BuildingType;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.trainingcamp.TrainingcampConstant;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;
	
	/**
	 *训练营 
	 * @author Administrator
	 * 
	 */	
	public class TrainingcampProxy extends BaseProxy
	{
		/**
		 *俱乐部proxy名字 
		 */
		public static const PROXYNAME:String = "TrainingcampProxy_proxyname";
		/**
		 * 
		 */
		private var _trainingCampDataManger:TrainingcampDataManager;
		
		/**
		 *是否在球员升级时出升级动画 （训练时）
		 */
		private var _isShowPlayerUp:Boolean;
		public function TrainingcampProxy(proxyName:String=PROXYNAME, data:Object=null)
		{
			super(proxyName, data);
			_trainingCampDataManger = AppData.getInstance().trainingCampDataManager;
		}
		/**
		 *接受 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		override public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case MainCommandType.MAINDEAL:
				{
					return onDeal(subCmdID,buffer,dataSize);
				}
				case MainCommandType.PUBLICCOMMANDTYPE:
				{
					return onDealPublic(subCmdID,buffer,dataSize);
				}
					
					
					
			}
			return false;
		}
		/**
		 *处理 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 * 
		 */		
		private function onDeal(subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				case SMDTraining.SUB_START_TRINING:
					getStartTraining(buffer);
					return true;
				
				
			}
			return false;
		}
		private function onDealPublic(subCmdID:uint,
									  buffer:ByteArray, 
									  dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				case SMDTraining.BUY_TRAINING_TYPE:
					getAddTriningTime(buffer);
					return true;
				case SMDTraining.CLEAR_TIME:
					getClearUpTime(buffer);
					return true;
				case SMDTraining.PLAYER_UP:
					getPlayerUp(buffer);
					return true;
			}
			return false;
		}
		/**
		 *请求训练信息 
		 * 
		 */
		public function requestTrainingInfo():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.TRAININGINFO;
			ProxyUtils.instance.webClient(getInfo,variables,false);
		}
		/**
		 *获得俱乐部信息 
		 * @param $data
		 * 
		 */
		private function getInfo($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,TrainingcampProxy);
			if(obj.retcode == 0)
			{
				_trainingCampDataManger.trainingcampInfoVo(obj);
				this.sendNotification(TrainingcampConstant.GET_TRAININGCAMPINFO);
			}else
			{
				Debug.echo("++++++++"+ErrorTips.instance.getErrorTip(obj.msg));
			}
		}
		/**
		 *请求训练  
		 * @param $obj
		 * 
		 */
		public function requestStartTraining($obj:Object):void
		{
			_isShowPlayerUp = true;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 5,MainCommandType.MAINDEAL,SMDTraining.SUB_START_TRINING);
			sendBuf.writeInt($obj.playerid);
			sendBuf.writeByte($obj.type);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回训练  
		 * @param buffer
		 * 
		 */
		private function getStartTraining(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				_trainingCampDataManger.trainingcampInfo.changeTime(buffer);
				
				this.sendNotification(TrainingcampConstant.GET_START_TRAINING,{status:true});
				Alert.showErrorTip("训练成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(TrainingcampConstant.GET_START_TRAINING,{status:false});
			}
		}
		/**
		 *升级训练营 
		 * @param buffer
		 * 
		 */
		private function getUpTraining(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				//				var id:int = buffer.readInt();
				_trainingCampDataManger.trainingcampInfo.level = buffer.readByte();
				_trainingCampDataManger.trainingcampInfo.coin = buffer.readInt();
				_trainingCampDataManger.trainingcampInfo.needlevel = buffer.readShort();
				var curtime :Number = AppData.getInstance().time;
				_trainingCampDataManger.trainingcampInfo.pretime = curtime;
				_trainingCampDataManger.trainingcampInfo.updataMax(buffer);
				this.sendNotification(TrainingcampConstant.GET_UP_TRAININGCAMP,{status:true});
				Alert.showErrorTip("升级成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				//				this.sendNotification(ClubProxyConstant.GET_CHANGESHIRT,{status:false});
				this.sendNotification(TrainingcampConstant.GET_UP_TRAININGCAMP,{status:false});
			}
		}
		/**
		 *请求冷却时间 
		 * @param $obj
		 * 
		 */
		public function requestClearUpTime($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 1,MainCommandType.PUBLICCOMMANDTYPE,SMDTraining.CLEAR_TIME);
			sendBuf.writeByte($obj.type);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回冷却时间 
		 * 
		 */
		public function getClearUpTime(buffer:ByteArray):void
		{
//			Alert.showErrorTip("时间冷却成功");
//			_trainingCampDataManger.trainingcampInfo.pretime = 0;
//			
//			this.sendNotification(TrainingcampConstant.GET_UP_TRAININGCAMP);
			
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				var type:int = buffer.readByte();
				_trainingCampDataManger.trainingcampInfo.clearTime(type);
				this.sendNotification(TrainingcampConstant.GET_UP_TRAININGCAMP,{status:true});
				Alert.showErrorTip("冷却成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(TrainingcampConstant.GET_UP_TRAININGCAMP,{status:false});
			}
		}
		/**
		 *w使用欧元增加次数 
		 * @param $obj
		 * 
		 */
		public function addTriningTime($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,MainCommandType.PUBLICCOMMANDTYPE,SMDTraining.BUY_TRAINING_TYPE);
//			sendBuf.writeByte($obj.type);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回增加次数 
		 * @param buffer
		 * 
		 */
		private function getAddTriningTime(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				
				
				_trainingCampDataManger.trainingcampInfo.addTrainingCount(buffer.readInt());
				this.sendNotification(TrainingcampConstant.GET_UP_TRAININGCAMP,{status:true});
				Alert.showErrorTip("购买成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(TrainingcampConstant.GET_UP_TRAININGCAMP,{status:false});
			}
		}
		/**
		 *获取球员升级 
		 * @param buffer
		 * 
		 */
		private function getPlayerUp(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn ==0)
			{
				var len:int = buffer.readShort();
				var arr :Array = [];
				var temp:Array ;
				var obj:Object;
				for(var i:int = 0;i < len;i++)
				{
					var playerid:int = buffer.readInt();
					var playerVo:PlayerVo = AppData.getInstance().playerCenterDataManager.getplayerVoById(playerid);
				
					if(playerVo != null)
					{
//						obj = {};
						obj = playerVo.updataBaseValue(buffer);
//						obj.arr = temp;
						obj.playerid = playerid;
						arr.push(obj);
					}
					
				}
				_trainingCampDataManger.upPlayerArr = _trainingCampDataManger.upPlayerArr.concat( arr);
				this.sendNotification(NotificationConstants.PLAYER_UPGRADE,{show:_isShowPlayerUp});
				_isShowPlayerUp = false;
//				_trainingCampDataManger.trainingcampInfo.changeTime(buffer);
				//				this.sendNotification(TrainingcampConstant.GET_UP_TRAININGCAMP,{status:true});
				//				Alert.showErrorTip("购买成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				//				this.sendNotification(TrainingcampConstant.GET_UP_TRAININGCAMP,{status:false});
			}
		}
	}
}